package com.stox.ui.javafx.charting.model.plot;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.stox.dto.AbstractModel;
import com.stox.dto.Bar;
import com.stox.dto.IndicatorModel;
import com.stox.indicator.Indicator;
import com.stox.ui.javafx.charting.model.uiunit.LineUiUnit;
import com.stox.ui.javafx.charting.strategy.PlotIndexUiUnitColorStrategy;

@Component("linePlot")
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class LinePlot extends NumberPlot{

	public LinePlot(AbstractModel model) {
		super(model);
	}
	
	@Autowired
	@SuppressWarnings("unchecked")
	public void setApplicationContext(ApplicationContext applicationContext){
		setColorStrategy(applicationContext.getBean(PlotIndexUiUnitColorStrategy.class));
	}
	
	@Override
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void update() {
		
		AbstractPrimaryPlot frontPlot = getChart().getChartingView().getPrimaryChart().getFrontPlot();
		List<Bar> bars = frontPlot.getData();
		if(frontPlot.isDataChanged() || bars.size() != getData().size()){
			if(IndicatorModel.INDICATOR_TYPE.isSubTypeOf(getModel().getType())){
				IndicatorModel indicatorModel = (IndicatorModel)getModel();
				Indicator<Object, Number> indicator = indicatorModel.getIndicator();
				setData(Arrays.asList(indicator.getValues(indicatorModel.getConfiguration(), bars)));
			}
		}
		
		super.update();
	}
	
	@Override
	protected void createUiUnits() {
		for(int i = 0; i < getData().size(); i++)			
			getUiUnits().add(new LineUiUnit(getData().get(i),this,i));
	}
	
}
